# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=enchant
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.1
pkgrel=1
pkgdesc="Enchanting Spell Checking Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://abiword.github.io/enchant/"
license=(LGPL2.1)
depends=(${MINGW_PACKAGE_PREFIX}-aspell
         ${MINGW_PACKAGE_PREFIX}-hunspell
         ${MINGW_PACKAGE_PREFIX}-nuspell
         ${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-glib2
         ${MINGW_PACKAGE_PREFIX}-libvoikko)
makedepends=("${MINGW_PACKAGE_PREFIX}-pkg-config")
checkdepends=("${MINGW_PACKAGE_PREFIX}-unittest-cpp")
options=('!libtool' 'strip' '!debug' '!makeflags')
source=("https://github.com/AbiWord/enchant/releases/download/v${pkgver}/enchant-${pkgver}.tar.gz"
        001_fix_relocation.patch
        002-providers-voikko-use-g_strndup.patch)
sha256sums=('7b4b1afcf2cd8bfa691deea6188404d337f23174bbc39b9c2add2bf340736e9c'
            '8cac1be09ab84212b4ee388cbaa45593e3ed71b257b64c20e99cf147249dec62'
            '42bb9377b1ef27bb49da36d3d1873deb9082f2ccb3b7e88a9e56d5e1fd671977')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001_fix_relocation.patch

  # https://github.com/AbiWord/enchant/pull/282
  patch -p1 -i ${srcdir}/002-providers-voikko-use-g_strndup.patch

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-relocatable \
    --with-aspell \
    --with-voikko \
    --without-hspell \
    --without-zemberek

  # MSYS2_ARG_CONV_EXCL="-DENCHANT_USPELL_DICT_DIR=;-DENCHANT_MYSPELL_DICT_DIR=;-DENCHANT_ISPELL_DICT_DIR=;-DENCHANT_PREFIX_DIR=;-DENCHANT_GLOBAL_MODULE_DIR=;-DENCHANT_GLOBAL_ORDERING=;-DENCHANT_BIN_DIR="
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  # MSYS2_ARG_CONV_EXCL="-DENCHANT_USPELL_DICT_DIR=;-DENCHANT_MYSPELL_DICT_DIR=;-DENCHANT_ISPELL_DICT_DIR=;-DENCHANT_PREFIX_DIR=;-DENCHANT_GLOBAL_MODULE_DIR=;-DENCHANT_GLOBAL_ORDERING=;-DENCHANT_BIN_DIR="
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB"
}
